package com.hand.wfive.fragment

import android.content.Context
import androidx.activity.result.ActivityResultLauncher
import com.bumptech.glide.Glide
import com.hand.wfive.R
import com.hand.wfive.base.BaseFragment
import com.hand.wfive.util.click.singleClick
import com.doris.media.picker.model.MediaPickerParameter
import com.doris.media.picker.result.contract.MediaPickerContract
import kotlinx.android.synthetic.main.fragment_test.*

class TestFragment : BaseFragment() {

    private lateinit var pickerMedia: ActivityResultLauncher<MediaPickerParameter>

    override fun getLayoutId() = R.layout.fragment_test

    override fun onAttach(context: Context) {
        super.onAttach(context)
        pickerMedia = registerForActivityResult(MediaPickerContract()) {
            if (it.isPicker) {
                when (it.requestCode) {
                    1 -> {
                        showNormalTip(topBar, it.getFirstPath())
                        Glide.with(this).load(it.getFirstPath()).into(ivTestImage);
                    }
                    2 ->{
                        showNormalTip(topBar, "选择了${it.size()}张图片")
                        Glide.with(this).load(it.getData()[it.size()-1].path).into(ivTestImage);
                    }
                    3 -> showNormalTip(topBar, "选择了${it.size()}个视频")
                    4 -> showNormalTip(topBar, "选择了${it.size()}个音频")
                }
            }
        }
    }

    override fun initKotlinWidget() {
        topBar.setTitle(R.string.app_name)

        btn_picker1.singleClick {
            pickerMedia.launch(MediaPickerParameter().requestCode(1))
        }
        btn_picker2.singleClick {
            pickerMedia.launch(MediaPickerParameter().max(9).requestCode(2))
        }
        btn_picker3.singleClick {
            pickerMedia.launch(
                MediaPickerParameter().video().min(2).max(9).requestCode(3)
            )
        }
        btn_picker4.singleClick {
            pickerMedia.launch(
                MediaPickerParameter().audio().min(2).max(9)
                    .spanCount(1).requestCode(4)
            )
        }
    }

}